(function($) {
	var $this = null;
	
	$(function() {
		var scheduleTypeDivHtm = "<div id=\"scheduleTypeListPanel_yinying\" class=\"yinying\" style='z-index:900;'></div><div id=\"scheduleTypeListPanel\" class=\"hideDiv\" style='z-index:901'>" + "<div style=\"float:right;margin-bottom:5px;width:20px;height:20px;line-height:20px;" + "text-align:center;background-color:#eee;margin-bottom:5px;border:1px solid #ccc;\" >" + " <span style=\"cursor: pointer;font-size: 14px;\"  id=\"scheduleTypeListPanel_close\" >X</span>" + " </div><table cellpadding=\"4\" cellspacing=\"0\" border=\"1\" width=\"100%\">" + "<thead><tr><th>服务项目名称</th><th>操作</th></tr></thead> <tbody id='scheduleTypeListPanel_data'></tbody> </table><div id=\"scheduleTypePager\" class='shopPager'></div></div>";
		$("body").prepend(scheduleTypeDivHtm);
		$("#scheduleTypeListPanel_close").bind("click", closeScheduleTypeListPanel);
		// 关闭
		$("#scheduleTypeListPanel_search").bind("click", search);
		// 查询
	});
	/**
	 * 显示商铺列表层
	 */
	function openScheduleTypeListPanel(self){
		$this = $(self);
		$this.blur();
		search();
	}

	/**
	 * 加载数据
	 */
	function search() {
		new Pagination({
			method : 'get', // 请求方法 post|get
			type : 'json', // 返回内容格式，json|text //NOT SUPPORT
			url : '../scheduleType/find/schdeuletTypeList', // 请求地址，注意函数会自定添加分页的两个参数 ‘0/10’
			// 使用post 方法是的请求参数
			pageSize : 10, // 页面大小
			startIndex : 0, // 服务器提供的接口页码是从几开始的，建议从0开始
			success : printDataToPage, // state 为 0 时，回调
			pagerHandler : 'scheduleTypePager', // 显示分页的 div 的 id
			last : last
		});
	}

	function last(message) {
		if(message.result.len==0){
			alert("暂无可用的优惠券");
			return false;
		}
		$("#scheduleTypeListPanel, #scheduleTypeListPanel_yinying").show();
		addMargin($("#scheduleTypeListPanel"));
	}

	function printDataToPage(message) {

		var $tbody = $("#scheduleTypeListPanel_data");
		$tbody.empty();
		
		// 清空数据
		var len = message.result.data.length;
		// 返回条目数
		
		for (var i = 0; i < len; i++) {
			// "id", "name","mp","contact");
			var ID = getHtmlString(getHeadIndexValue(i, message, 'ID'));
			var Name = getHtmlString(getHeadIndexValue(i, message, 'Name'));

			var select = "<td><input class=\"common_btn\" type=\"button\" onclick=\"__scheduleTypeSelected('" + ID + "','" + Name + "')\" value=\"选择\"></input></td>";

			var tr = "<tr>" + "<td>" + Name + "</td>"  + select + "</tr>";
			$tbody.append($(tr));
		}
	}

	/**
	 * 关闭
	 */
	function closeScheduleTypeListPanel() {
		$("#scheduleTypeListPanel, #scheduleTypeListPanel_yinying").hide();
	}

	/**
	 * 选择并关闭
	 */
	function __scheduleTypeSelected(scheduleTypeId, scheduleTypeName) {
		$this.empty();
		$this.append("<option value='" + scheduleTypeId + "' selected=\"selected\" >" + scheduleTypeName + "</option>");
		$("#scheduleTypeListPanel, #scheduleTypeListPanel_yinying").hide();

		if (window.scheduleTypeSelected) {
			window.scheduleTypeSelected(scheduleTypeId, scheduleTypeName);
		}
	}


	window.__scheduleTypeSelected = __scheduleTypeSelected;
	window.openScheduleTypeListPanel = openScheduleTypeListPanel;
})(jQuery);
